/**
 * @author David Milne
 */

function SearchBox() {
	var txtSearch ;
	var cmdSearch ;
	var cmdShowPrev ;
	var prevQueries ;
	
	var recentQueries ;
	var bestQueries ;
}

SearchBox.prototype.init = searchBox_init ;
SearchBox.prototype.resize = searchBox_resize ;
SearchBox.prototype.getCurrentQuery = searchBox_getCurrentQuery ;
SearchBox.prototype.setFocus = searchBox_setFocus ;
SearchBox.prototype.processKeyPress = searchBox_processKeyPress ;

SearchBox.prototype.showPreviousQueries = searchBox_showPreviousQueries ;
SearchBox.prototype.hidePreviousQueries = searchBox_hidePreviousQueries ;
SearchBox.prototype.selectQuery = searchBox_selectQuery ;


function searchBox_init() {
	try {
		this.className = "searchBox" ;
		
		this.txtSearch = document.createElement("input") ;
		this.txtSearch.type = "text" ;
		this.txtSearch.onclick = new Function("koru.searchBox.hidePreviousQueries()") ;
		this.txtSearch.onkeypress = new Function("event", "koru.searchBox.processKeyPress(event) ;") ;
		this.appendChild(this.txtSearch) ;
		
		this.cmdShowPrev = document.createElement("div") ;
		this.cmdShowPrev.className = "cmdShowPrev" ;
		this.cmdShowPrev.onclick = new Function("koru.searchBox.showPreviousQueries()") ;
		this.appendChild(this.cmdShowPrev) ;
		
		this.prevQueries = document.createElement("table") ;
		this.prevQueries.className = "prevQueries" ;
		this.prevQueries.onmouseout = new Function("event", "if (isMouseLeaveOrEnter(event, this)) koru.searchBox.hidePreviousQueries()") ;
		this.appendChild(this.prevQueries) ;
		
		this.cmdSearch = document.createElement("div") ;
		this.cmdSearch.className = "cmdSearch" ;
		this.cmdSearch.type = "button" ;
		this.cmdSearch.value = "Search" ;
		this.cmdSearch.onclick = new Function("koru.queryResultsTray.handleQueryChanged()") ;
		this.appendChild(this.cmdSearch) ;
	} catch (e) {
		logError("searchBox.init.error: " + e ); 
	}
}

function searchBox_resize(width) {
	try {
		
		this.style.width = width + "px" ;
		
		this.txtSearch.style.width = (width-156) + "px" ;
		this.prevQueries.style.width = (width-131) + "px"
		
		
	} catch (e) {
		logError("searchBox.resize.error: " + e ); 
	}	
}

function searchBox_getCurrentQuery() {
	try {
		return this.txtSearch.value ;
	} catch (e) {
		logError("searchBox.getCurrentQuery.error: " + e ); 
	}		
}

function searchBox_setFocus() {
	try {
		logDebug("setting focus") ;
		this.txtSearch.select() ;
		this.txtSearch.focus() ;
	} catch (e) {
		logError("searchBox.setFocus.error: " + e ); 
	}	
	
}

function searchBox_processKeyPress(keyEvent){
	try {
		if (document.all) {
   			keyEvent = window.event;
  		}
    
   		var key;
   		key = keyEvent.keyCode
	 
		if (key==10 || key==13){
	  		koru.queryResultsTray.handleQueryChanged()
	  	}
	} catch(e) {
		logError( "searchBox.processKeyPress.error: " + e ); 
		throw (e) ;	
	}
}

function searchBox_showPreviousQueries() {
	
	if (this.prevQueries.style.display != "none")
		this.hidePreviousQueries() ;
	else {
		
		this.prevQueries.innerHTML = "" ;
		
		var row = this.prevQueries.insertRow(this.prevQueries.rows.length) ;
		var cell = row.insertCell(0) ;
		cell.innerHTML = "Recent Queries:" ;
		row.className = "header" ;
				
		for(var i=0 ; i< this.recentQueries.childNodes.length ; i++) {
			var query = this.recentQueries.childNodes[i] ;
		
			row = this.prevQueries.insertRow(this.prevQueries.rows.length) ;
			row.onclick = new Function("koru.searchBox.selectQuery(\"" + escape(query.getAttribute("text")) + "\")")			
			row.className = "normal" ;
			
			cell = row.insertCell(0) ;
			cell.innerHTML = query.getAttribute("text") ;
			
			cell = row.insertCell(1) ;
			cell.innerHTML = query.getAttribute("score") ;
			cell.className = "score" ;
		}
		
		row = this.prevQueries.insertRow(this.prevQueries.rows.length) ;
		cell = row.insertCell(0) ;
		cell.innerHTML = "Best Queries:" ;
		row.className = "header" ;
		
		for(var i=0 ; i< this.bestQueries.childNodes.length ; i++) {
			var query = this.bestQueries.childNodes[i] ;
		
			row = this.prevQueries.insertRow(this.prevQueries.rows.length) ;
			row.onclick = new Function("koru.searchBox.selectQuery(\"" + escape(query.getAttribute("text")) + "\")")			
			row.className = "normal" ;
			
			cell = row.insertCell(0) ;
			cell.innerHTML = query.getAttribute("text") ;
			
			cell = row.insertCell(1) ;
			cell.innerHTML = query.getAttribute("score") ;
			cell.className = "score" ;
		}
		
		this.prevQueries.style.display = "block" ;
	}
}

function searchBox_hidePreviousQueries() {
	this.prevQueries.style.display = "none" ;
}

function searchBox_selectQuery(query) {
	this.hidePreviousQueries() ;
	this.txtSearch.value = unescape(query) ;
	koru.queryResultsTray.handleQueryChanged() ;
}